package com.fable.web.controller.customer;

import com.fable.biz.operate.CustomerSourceService;
import com.fable.biz.operate.dto.CustomerSourceDTO;
import com.fable.biz.operate.query.CustomerSourceQueryCondition;
import com.fable.common.config.RequireLogin;
import com.fable.common.enums.BusiErrCode;
import com.fable.common.utils.JsonResult;
import com.fable.dal.model.CustomerSourceDO;
import com.fable.utils.StringUtil;
import com.fable.web.page.P;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 客户来源
 * @ClassName CustomerSourceController
 * @Description TODO
 * @Author aaters
 * @Date 2018/7/24 下午7:34
 * @Version 1.0
 **/
@Controller
@RequireLogin
@RequestMapping("/customerSource")
public class CustomerSourceController {

    @Autowired
    private CustomerSourceService customerSourceService;

    /**
     * 客户来源列表
     *
     * @return
     */
    @RequestMapping("/customerSourceList.htm")
    public String customerType(ModelMap modelMap, CustomerSourceQueryCondition condition) {
        List<CustomerSourceDTO> customerSourceDTOS = customerSourceService.querySourceAll(condition);
        modelMap.put("query", condition);
        modelMap.put("customerSource", customerSourceDTOS);
        return P.Common.customerSourceList;
    }

    /**
     * 跳转到添加或修改客户来源页面
     *
     * @param modelMap
     * @param id
     * @return
     */
    @RequestMapping("addOrModifyCustomerSource.htm")
    public String addOrModifyCustomerSource(ModelMap modelMap, Integer id) {
        modelMap.put("result", customerSourceService.querySourceById(id));
        return P.Common.addCustomerSource;
    }

    /**
     * 添加或修改客户来源
     *
     * @param customerSourceDTO
     * @return
     */
    @RequestMapping("/addOrModifyCustomerSource.json")
    @ResponseBody
    public JsonResult addOrModifyCustomerSource(@ModelAttribute CustomerSourceDTO customerSourceDTO) {
        JsonResult jsonResult = new JsonResult();
        if (StringUtil.isBlank(customerSourceDTO.getSourceName()) && StringUtil.isBlank(customerSourceDTO.getSourceDescribe())) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
        }
        int count = customerSourceService.addOrModifyCustomerSource(customerSourceDTO);
        if (count == 0) {
            jsonResult.setErrCode(BusiErrCode.INNER_ERROR);
        }
        return jsonResult;
    }

    @RequestMapping("/delCustomerSource.json")
    @ResponseBody
    public JsonResult delCustomerSource(Integer id) {
        JsonResult jsonResult = new JsonResult();
        if (id != null) {
            CustomerSourceDO customerSourceDO = customerSourceService.querySourceById(id);
            if (customerSourceDO == null) {
                jsonResult.setErrMsg("对象不存在!");
                return jsonResult;
            } else {
                CustomerSourceDTO customerSourceDTO = new CustomerSourceDTO();
                customerSourceDTO.setId(id);
                customerSourceDTO.setStatus(-1);
                customerSourceService.addOrModifyCustomerSource(customerSourceDTO);
            }
        }
        return jsonResult;
    }
}
